package ydn.edu.hqu.cst.myexperiment_8;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.VideoView;
import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private VideoView videoView;
    private ImageButton play,pause,stop;
    private Button search;
    private static final int FILE_SELECT_CODE=1;
    private static final String TAG="VideoActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        videoView=findViewById(R.id.video_view);
        //按钮的初始化
        play=findViewById(R.id.play);
        pause=findViewById(R.id.pause);
        stop=findViewById(R.id.stop);
        search=findViewById(R.id.search) ;
        //为“开始”、“暂停”、“停止”、“查找”按钮绑定事件监听器
        search.setOnClickListener(this);
        play.setOnClickListener(this);
        pause.setOnClickListener(this);
        stop.setOnClickListener(this);
        if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);//判断你是否授权
        }
        else {
            inintVideoPath();
        }
    }
    private void inintVideoPath(){
        File file=new File(Environment.getExternalStorageDirectory(),"158769137.mp4");//打开软件直接播放的视频名字是movie.mp4
        videoView.setVideoPath(file.getPath());//指定视频文件的路径
    }
    //各按钮的功能
    public void onClick(View v){
        switch (v.getId()){
            //点击“播放”按钮
            case R.id.play:
                if(!videoView.isPlaying()){
                    videoView.start();
                }
                break;
            //点击“暂停”按钮
            case R.id.pause:
                if(videoView.isPlaying()){
                    videoView.pause();
                }
                break;
            //点击“停止”按钮
            case R.id.stop:
                if(videoView.isPlaying()){
                    videoView.stopPlayback();
                }
                break;
            //点击“查找”按钮
            case R.id.search:
                Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");//设置类型，这是任意类型
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent,1);
        }
    }
    //释放资源
    public void onDestroy(){
        super.onDestroy();
        if(videoView!=null){
            videoView.suspend();
        }
    }
    public void onActivityResult(int requestCode,int resultCode,Intent data){
        if(resultCode== Activity.RESULT_OK){
            Uri uri=data.getData();
            videoView.setVideoURI(uri);//将选择的文件路径给播放器
            super.onActivityResult(requestCode, resultCode, data);
            return;
        }
        if (requestCode == FILE_SELECT_CODE) {
            Uri uri = data.getData();
            Log.i(TAG, "------->" + uri.getPath());
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
